# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: threading_queue.py 
@time: 2019/7/31 下午1:55
@note:
通过队列实现线程之间通信
1.设置关键值让消费者读该数据将终止执行
"""

from queue import Queue
from threading import Thread

# 设置关键值让消费者读该数据将终止执行
_sentinel = object()

# 第一个线程中的生产者数据
def producer(out_q):

    data = ('a', 'b')
    while True:
        out_q.put(data)

    out_q.put(_sentinel)

# 第二个线程中的消费者数据
def consumer(in_q):

    while True:
        data = in_q.get()
        print('Consumer recv producer data:',data)

        if data is _sentinel:
            in_q.put(_sentinel)
            break


q = Queue()
t1 = Thread(target=consumer, args=(q,))
t2 = Thread(target=producer, args=(q,))

t1.start()
t2.start()
